package com.xiaoxx.rabbitmq.bean;



import com.xiaoxx.rabbitmq.constant.MessageType;
import com.xiaoxx.rabbitmq.constant.RabbitQueueExchangeConstant;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 消息封装对象
 * @author quhui
 */
@Data
public class MyMessage<T> implements Serializable {

    private static final long serialVersionUID = 1009338994944144327L;
    
    /* 	消息的唯一ID	*/
    private String messageId;

    /*	消息的交换机		*/
    private String topic;

    /*	消息的路由规则	*/
    private String routingKey = "";

    /*	消息体	*/
    private T data;

    /*	消息发送时间 	*/
    private String sendTime;

    /*	延迟消息的参数配置 单位(秒)	*/
    private Integer delayMills;

    /*	消息类型：默认为confirm消息类型	*/
    private String messageType = MessageType.CONFIRM;

    public MyMessage() {
    }

    public MyMessage(String messageId, String topic, String routingKey, T data) {
        this(messageId, topic, routingKey, data, null, null);
    }

    public MyMessage(String messageId, String topic, String routingKey, T data, String messageType) {
        this(messageId, topic, routingKey, data, null, messageType);
    }

    public MyMessage(String messageId, String topic, String routingKey, T data, Integer delayMills) {
        this(messageId, topic, routingKey, data, delayMills, null);
    }

    public MyMessage(String messageId, String topic, String routingKey, T data, Integer delayMills,
                     String messageType) {
        if (StringUtils.hasText(messageId)) {
            this.messageId = messageId;
        } else {
            this.messageId = UUID.randomUUID().toString().replace("-", "");
        }
        this.sendTime = LocalDateTime.now().format(RabbitQueueExchangeConstant.DATE_TIME_FORMATTER);
        this.topic = topic;
        this.routingKey = routingKey;
        this.data = data;
        this.delayMills = delayMills;
        if (messageType != null){
            this.messageType = messageType;
        }
    }

}
